package com.cait.cait.controller;

import com.cait.cait.entity.Member;
import com.cait.cait.entity.MemberCategory;
import com.cait.cait.model.MemberForm;
import com.cait.cait.util.ResultVoUtil;
import com.cait.cait.vo.MemberCategoryVo;
import com.cait.cait.vo.MemberMsgVo;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Jirath
 * @date 2020/2/13
 * @description:
 */
@Controller
@RequestMapping("/admin")
public class MemberAdminController extends BasicController{

    @GetMapping("/member")
    public ModelAndView toAdminMember(@RequestParam(value = "memberId",required = false) Integer memberId){
        ModelAndView view = new ModelAndView("/admin/member");

        //****************假数据****************
        class data{
            Object memberList;
            Object member;
            Object memberCategoryList;

            data(Object member,Object memberList,Object memberCategoryList){
                this.member = member;
                this.memberList = memberList;
                this.memberCategoryList = memberCategoryList;
            }

            public Object getMemberList() {
                return memberList;
            }

            public void setMemberList(Object memberList) {
                this.memberList = memberList;
            }

            public Object getMember() {
                return member;
            }

            public void setMember(Object member) {
                this.member = member;
            }

            public Object getMemberCategoryList() {
                return memberCategoryList;
            }

            public void setMemberCategoryList(Object memberCategoryList) {
                this.memberCategoryList = memberCategoryList;
            }
        }
        List<MemberMsgVo> memberList = new ArrayList<>();
        List<MemberCategoryVo> memberCategoryList = new ArrayList<>();
        for(int i=1;i<=10;i++){
            MemberMsgVo member = new MemberMsgVo();
            member.setMemberId(i);
            member.setMemberGrade("grade"+i);
            member.setMemberIntroduce("introduce"+i);
            member.setMemberMajor("major"+i);
            member.setMemberName("name"+i);
            member.setMemberPicture("pic"+i);
            member.setMemberStudentNum("num"+i);
            member.setMemberCategoryName("职位"+i);
            member.setCreateTime(new Date());
            member.setLastModifiedTime(new Date());
            memberList.add(member);
        }
        for (int i=1;i<5;i++){
            MemberCategoryVo memberCategory = new MemberCategoryVo();
            memberCategory.setMemberCategoryId(i);
            memberCategory.setMemberCategoryName("类目"+i);
            memberCategoryList.add(memberCategory);
        }
        //有id就查询该id对应的值，没有id代表不修改,传空值。
        MemberMsgVo member = null;
        if(memberId != null){
            member = new MemberMsgVo();
            member.setMemberId(memberId);
            member.setMemberGrade("grade"+memberId);
            member.setMemberIntroduce("introduce"+memberId);
            member.setMemberMajor("major"+memberId);
            member.setMemberName("name"+memberId);
            member.setMemberPicture("pic"+memberId);

            member.setMemberCategoryId(memberId);
            member.setMemberCategoryName("职位"+memberId);
            member.setMemberStudentNum("num"+memberId);
            member.setCreateTime(new Date());
            member.setLastModifiedTime(new Date());
            member.setMemberCategoryId(memberId);
        }


        view.addObject("result",ResultVoUtil.success(200,"列表数据",new data(member,memberList,memberCategoryList)));
        //****************假数据****************

        return view;
    }

    /**
     * 结束信息类Member，除去id和时间
     * @param memberForm
     * @return
     */
    @PostMapping("/member/save")
    public ModelAndView addMember(@Valid MemberForm memberForm, BindingResult bindingResult){
        ModelAndView view=new ModelAndView("/result");
        if(bindingResult.hasErrors()){
            logger.error("参数不正确：{}",memberForm);
            List<String> fieldErrorList = new ArrayList<>();
            for (FieldError error : bindingResult.getFieldErrors()) {
                fieldErrorList.add(error.getDefaultMessage());
            }
            view.addObject("result", ResultVoUtil.success(500,"参数不正确",fieldErrorList.toString()));
            return view;
        }
        System.out.println(memberForm);
        //修改操作...
        try {

        }catch (Exception e){

        }finally {

        }
        view.addObject("result", ResultVoUtil.success(200,"操作成功",null));
        return view;
    }

    @GetMapping("/member/delete")
    public ModelAndView deleteMember(@RequestParam("memberId") Integer memberId){
        ModelAndView view=new ModelAndView("/result");

        //删除操作...
        try {

        }catch (Exception e){

        }finally {

        }


        view.addObject("result", ResultVoUtil.success(200,"删除成功",null));
        return view;
    }
}
